package com.maize.uwwireless;

import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.PreferenceActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class Settings extends PreferenceActivity implements OnSharedPreferenceChangeListener
{	
	public static final String USERNAME = "username_field";
	public static final String PASSWORD = "password_field";
	
	private final int CLEAR = Menu.FIRST;
	private final int WIFI = Menu.FIRST + 1;
	private final int ABOUT = Menu.FIRST + 2;
		
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);     
        this.setTitle(R.string.title);   
        addPreferencesFromResource(R.xml.preferences);
    }
    
    @Override
    protected void onResume() 
    {
        super.onResume();
        
    	updateUsername();
        updatePassword();
        // updateConnectionStatus();
        
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }
    
    @Override
    protected void onPause() 
    {
        super.onPause();
        // Unregister the listener whenever a key changes
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);    
    }
    
    public boolean onCreateOptionsMenu(Menu menu)
    {
    	super.onCreateOptionsMenu(menu);
    	MenuItem item = menu.add(0, CLEAR, 0, "Clear Data");
    	item.setIcon(R.drawable.clear);
    	item = menu.add(0, WIFI, 0, "Wi-Fi");
    	item.setIcon(R.drawable.cloud);
    	item = menu.add(0, ABOUT, 0, "About");
    	item.setIcon(R.drawable.about);
		return true;    	
    }
    
    public boolean onMenuItemSelected(int featureId, MenuItem item) 
	{
    	switch( item.getItemId() ) 
        {		
        	case CLEAR:
        		EditTextPreference et = (EditTextPreference) getPreferenceManager().findPreference(USERNAME);
        		et.setText("");
        		et.setSummary("");
        		et = (EditTextPreference) getPreferenceManager().findPreference(PASSWORD);
        		et.setText("");
        		et.setSummary("");
            	Toast.makeText(this, "User data cleared", Toast.LENGTH_SHORT).show();
        		return true;
        		
        	case WIFI:
        		Intent i = new Intent(Intent.ACTION_MAIN);
        		i.setClassName("com.android.settings", "com.android.settings.wifi.WifiSettings");
            	this.startActivity(i);
            	return true;
        		
        	case ABOUT:
        		AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setIcon(R.drawable.icon);
                builder.setTitle("Waterloo for Android");
                builder.setMessage(R.string.about);
                builder.setNeutralButton("OK", null);
                builder.show();
        		return true;
        }
		return true;    	
	}
	
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) 
    {
        if (key.equals(USERNAME)) 
        {
            updateUsername();
        }
        else if (key.equals(PASSWORD)) 
        {
        	updatePassword();
        }
    }
    
    private void updateUsername()
    {
    	String summary = "";
    	
    	EditTextPreference et = (EditTextPreference) getPreferenceManager().findPreference(USERNAME);
    	try
    	{    		
    		summary = et.getText();
    		et.setSummary(summary);
    	}
    	catch (Exception e)
    	{
        	et.setSummary("");
    	}
    	
    }
    
    private void updatePassword()
    {
    	String summary = "";
    	
    	EditTextPreference et = (EditTextPreference) getPreferenceManager().findPreference(PASSWORD);
    	try
    	{
        	summary = et.getText();
        	String pwd = "";
    		for (int i =0; i< summary.length(); i++)
        	{
        		pwd += "*";
        	}
        	et.setSummary("" + pwd);
    	}
    	catch (Exception e)
    	{
        	et.setSummary("");
    	}
    }
    
    /*
    private void updateConnectionStatus()
    {
        WifiManager a = (WifiManager) getSystemService(WIFI_SERVICE);
    	EditTextPreference et = (EditTextPreference) getPreferenceManager().findPreference(IP);
        //DhcpInfo info = a.getDhcpInfo();        
        
        if (a.isWifiEnabled())
        {
        	et.setSummary(a.getConnectionInfo().getIpAddress());        	
        }
        else
        {
        	et.setSummary("No connection");
        }        
        et = (EditTextPreference) getPreferenceManager().findPreference(MAC);
        et.setSummary(Settings.System.ANDROID_ID);//a.getConnectionInfo().getMacAddress());
    	//et = (EditTextPreference) getPreferenceManager().findPreference(MAC);
    	//et.setSummary(a.getConnectionInfo().getMacAddress());
    }
    */
}